package de.dlrg.wms.dummyDevice.controller;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Button;
import org.springframework.stereotype.Component;

import de.dlrg.wms.dummyDevice.DummyDevice;
import de.dlrg.wms.interfaces.controller.IDeviceController;
import de.dlrg.wms.interfaces.controller.IDeviceControllerCallback;
import de.dlrg.wms.interfaces.controller.StopperEvent;

import wms2010.interfaces.devices.IDevice;

@Component
public class DummyDeviceController extends IDeviceController implements
		SelectionListener {
	private ArrayList<IDevice> devices = new ArrayList<IDevice>();
	private ArrayList<IDeviceControllerCallback> iDeviceControllerCallbacks = new ArrayList<IDeviceControllerCallback>();

	private DummyDevice createDummyDevice(String name) {
		DummyDevice device = new DummyDevice(name);
		device.getDeviceGui().btnStop.addSelectionListener(this);
		this.devices.add(device);
		return device;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * de.dlrg.wms.dummyDevice.controller.IDeviceController#startDiscovery()
	 */
	public void startDiscovery() {
		this.devices.clear();
		for (int i = 0; i < 3; i++) {
			this.createDummyDevice("DummyDevice #" + (i + 1));
		}

		this.createDummyDevice("DummyDevice starter").setStarter(true);
		this.notifyDeviceControllerCallback();
	}

	private void notifyDeviceControllerCallback() {
		for (IDeviceControllerCallback controllerCallback : this.iDeviceControllerCallbacks) {
			controllerCallback.finishedDiscovery();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see de.dlrg.wms.dummyDevice.controller.IDeviceController#stopDiscovery()
	 */
	public void stopDiscovery() {
		// do nothing
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see de.dlrg.wms.dummyDevice.controller.IDeviceController#getAllDevices()
	 */
	public List<IDevice> getAllDevices() {
		return devices;
	}

	public String getName() {
		return "Dummy Devices";
	}

	public void registerDeviceControllerCallback(
			IDeviceControllerCallback iDeviceControllerCallback) {
		if (this.iDeviceControllerCallbacks.contains(iDeviceControllerCallback)) {
			throw new ArrayIndexOutOfBoundsException(
					"Already registered such callback! Check your code!");
		}
		this.iDeviceControllerCallbacks.add(iDeviceControllerCallback);
	}

	public void removeDeviceControllerCallback(
			IDeviceControllerCallback iDeviceControllerCallback) {
		if (!this.iDeviceControllerCallbacks
				.contains(iDeviceControllerCallback)) {
			throw new ArrayIndexOutOfBoundsException(
					"Not such callback registered! Check your code!");
		}
		this.iDeviceControllerCallbacks.remove(iDeviceControllerCallback);
	}

	public void widgetDefaultSelected(SelectionEvent arg0) {

	}

	public void widgetSelected(SelectionEvent arg0) {
		Long time = System.currentTimeMillis();
		Button btnPressed = (Button) (arg0.getSource());
		IDevice device = (IDevice) btnPressed.getData();
		this.notifyButtonPressed(new StopperEvent().setDevice(device)
				.setTimeOfEvent(time));
	}
}
